// src/store/index.js
import { configureStore, combineReducers } from '@reduxjs/toolkit';
import userReducer from './userSlice';

import storage from 'redux-persist/lib/storage';
import { persistReducer, persistStore } from 'redux-persist';
import { FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER } from 'redux-persist';

// 1. 持久化配置
const persistConfig = {
  key: 'root',
  storage,
  whitelist: ['user'], // 只持久化 user 模块
};

// 2. 合并 reducer
const rootReducer = combineReducers({
  user: userReducer,
});

// 3. 创建持久化 reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

// 4. 创建 store（带中间件处理以解决非序列化警告）
export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        // 忽略 redux-persist 的这些 action 类型检查
        ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
      },
    }),
});

// 5. 创建持久化器
export const persistor = persistStore(store);
